# -*- coding: UTF-8 -*-

import os
import sys
import argparse

try:
    import markdown
except ImportError:
    sys.stderr.write("Markdown not found. Please install it.\n")
    exit()

try:
    import yaml
except ImportError:
    sys.stderr.write("Yaml for python not found. Please install it.\n")
    exit()

try:
    import jinja2
except ImportError:
    sys.stderr.write("Jinja2 not found. Please install it.\n")
    exit()

try:
    import magic
except ImportError:
    sys.stderr.write("Magic not found. Please install it.\n")
    exit()


from config import Config
from parser import ParserFile 


import datetime


class Page:
    """
    Doit être instancié lors de la génération d’une page.
    Permet entre autre d’avoir prev, next, dans le cas d’un post.
    """
    def __init__(self, options):
        pass


class Site:
    def __init__(self):
        self.time = datetime.datetime.now()
        self.posts = []
        self.pages = []
        self.categories = []
        self.tags = []
    
    def create_url(self):
        """
        Before start, check if is post or not, if post:
        First, look at headers file to check whether permalink is set
        Second, look at config file to check whether permalink is set
        Third, set url as default /year/month/day/title.html
        If not post, then only 2 steps:
        One: the same at first point for post case,
        Two: Take the FS path form projets root et take its file name.
        """
        pass

    def posts_prepare(self):
        """
        trier les posts par date
        mise en plce des URL Cf methode ci-dessus
        réfléchir au cas des prev/next… Comment faire pour coller au plus près de jekyll ?
        """
        pass

    def parse_only_headers(self):
        from os.path import join, getsize, dirname, basename
        import magic

        m = magic.open(magic.MAGIC_MIME)
        m.load()
        
        for root, dirs, files in os.walk('.'):

            root_name = basename(root)

            #TODO: think about way to remember what to copy or not.
            if(root_name.startswith('_')):
                #→ ne sera pas copié.
                pass
            else:
                # → sera copié.
                pass
            
            for name in files:
                file_name = join(root, name)

                if(name.startswith('_')):
                    # ne sera pas copié mais juste interprété
                    pass
                else:
                    # → sera copié et interprété si header yaml
                    pass

                mime = m.file(file_name)
                
                if(mime.startswith('text/') or mime.startswith('application/xml')):
                    p = ParserFile(file_name)
                    
                    #TODO: Warning about other type than page or post…
                    if(p.has_header()):
                        h = p.parse_header()
                        h.setdefault('path', file_name)
                        
                        if(root_name == '_posts'):
                            h.setdefault('is_post', True)
                            self.posts.append(h)
                        else:
                            h.setdefault('is_post', False)
                    else:
                        h = {}
                        h.setdefault('is_post', False)
                        h.setdefault('path', file_name)



site = Site()
site.parse_only_headers()
print site.posts
print len(site.posts)


"""
config = Config()
print config.get_tags_dir()
print config.options['tz_offset']

pf = ParserFile('test.markdown')
print pf.parse_header()
print pf.parse_content()
print pf.has_header()
print pf.has_content()
"""

parser = argparse.ArgumentParser(description='Text to create…')
parser.add_argument('--server', dest='server', action='store_true', help='run minimal server')
parser.add_argument('--no-future', dest='no-future', action='store_true', help='render future dated posts')
parser.add_argument('--version', dest='version', action='version', version='msbg 1.0', help='display current version')

args = parser.parse_args()
#print args.server
#print args.accumulate(args.integers)
